#!/usr/bin/env python
# -*- coding: utf-8 -*-

#
# This program is free software; you can redistribute it and/or modify  
# it under the terms of the GNU General Public License as published by  
# the Free Software Foundation; either version 2 of the License, or     
# (at your option) any later version.                                   
#                                                                         
# A copy of the license can be found in the license.txt file supplied   
# with this software or at: http://www.gnu.org/copyleft/gpl.html       
#


class   MalformedURLError(Exception) : pass



class   URL(object) :
    ''' Address of nodes.
    Once created, a URL instance is unmodifiable.
    '''

    def __init__(self, url_string, default_port=0) :
        ''' Create an instance of URL from <code>url_string</code>.

        @param urlString
                   The string to create an URL from.
        @throws MalformedURLException
                    This can occur if <code>url_string</code> does not match the
                    pattern <code>protocol://host[:port]/path</code>, an
                    unknown protocol is specified, or port is negative.
        '''
        self._default_port = default_port

        self._protocol, self._host, self._port, self._path = self.parse(url_string)

        self._url_string = url_string
        ## __init__()

    def parse(self, url_string) :
        url_str = url_string

        # parse protocol
        idx = url_str.find('://')
        if idx < 0 :
            raise MalformedURLError, 'Invalid URL "%s"' % url_string
        protocol = url_str[ : idx]
        protocol = protocol.lower()
        url_str = url_str[idx+3 : ]

        # parse host and port
        idx = url_str.find('/')
        if idx < 0 :
            idx = len(url_str)
            #raise MalformedURLError, 'Invalid URL "%s"' % url_string
        host = url_str[ : idx ]
        url_str = url_str[idx+1 : ]

        port = self._default_port
        idx = host.find(':')
        if idx > 0 :
            port = host[ idx + 1 : ]
            port = int(port)
            if port < 0 or port >= 65535 :
                raise MalformedURLError, 'Invalid URL "%s"' % url_string
            host = host[ : idx ]

        host = host.lower()

        # parse path
        path = url_str
        if not path :
            path = '/'

        return (protocol, host, port, path)
        ## parse()

    ####-----------------------------------------------------------####

    def getProtocol(self) :
        return self._protocol
        ## getProtocol()
    protocol = property(getProtocol)

    def getHost(self) :
        return self._host
        ## getHost()
    host = property(getHost)

    def getPort(self) :
        return self._port
        ## getPort()
    port = property(getPort)

    def getPath(self) :
        return self._path
        ## getPath()
    path = property(getPath)

    ####-----------------------------------------------------------####

    def __eq__(self, rhs) :
        if not isinstance(rhs, URL) :
            raise TypeError

        if self.protocol == rhs.protocol and self.host == rhs.host and self.port == rhs.port and self.path == rhs.path :
            return True

        return False
        ## __eq__()

    def __str__(self) :
        return '<URL %s://%s:%d/%s>' % (self._protocol, self._host, self._port, self._path)
        ## __str__()

    ## class URL


